package com.learning.datastruct.heap;

import java.util.Comparator;

/**
 * @author ZLH
 */
public abstract class MyAbstractHeap<E> implements MyHeap<E> {
    protected int size;
    protected Comparator<E> comparator;

    protected MyAbstractHeap(Comparator<E> comparator) {
        this.comparator = comparator;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @SuppressWarnings("unchecked")
    protected int compare(E e1, E e2) {
        return comparator != null ? comparator.compare(e1, e2)
                : ((Comparable<E>)e1).compareTo(e2);
    }
}
